//
//  HomePageHeaderView.m
//  HSSFinancial
//
//  Created by xinqiao on 2017/1/10.
//  Copyright © 2017年 Jon_G. All rights reserved.
//

#import "HomePageHeaderView.h"

#import "HuisangsuoApi.h"
#import "UIImageView+WebCache.h"

@implementation HomePageHeaderView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self = [[[NSBundle mainBundle]loadNibNamed:@"HomePageHeaderView" owner:self options:nil]lastObject];
    }
    return self;
}
- (void)setImageArray:(NSArray *)imageArray {
    self.pageControl.numberOfPages = imageArray.count-1;
    self.homePageScrollView.showsHorizontalScrollIndicator = NO;
    self.homePageScrollView.pagingEnabled=YES;
    self.homePageScrollView.bounces = NO;
    self.homePageScrollView.showsVerticalScrollIndicator=NO;
    self.homePageScrollView.delegate = self;
    self.homePageScrollView.contentSize=CGSizeMake(imageArray.count * self.homePageScrollView.frame.size.width, self.homePageScrollView.frame.size.height);
    for (int i=0; i<imageArray.count; i++) {
        UIImageView * imageview=[[UIImageView alloc]initWithFrame:CGRectMake(i*self.homePageScrollView.frame.size.width, 0,self.homePageScrollView.frame.size.width, self.homePageScrollView.frame.size.height)];
        imageview.userInteractionEnabled = YES;
        imageview.contentMode =  UIViewContentModeScaleAspectFill;
        imageview.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [imageview sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",[HuisangsuoApi shoppingMallUrlString],imageArray[i][@"pic"]]]];
        [self.homePageScrollView addSubview: imageview];
    }
    self.pageControl.numberOfPages=imageArray.count;
    _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    self.pageControl.userInteractionEnabled=NO;
    [self  addTimer];
}
#pragma mark ----------------- 图计时器 -----------
- (void)addTimer{
    if (self.timer!=NULL) {
        [self.timer invalidate];
        self.timer=NULL;
    }
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
}
- (void)nextImage {
    float offset_X = self.homePageScrollView.contentOffset.x;
    //每次切换一个屏幕
    offset_X += CGRectGetWidth(self.bounds);
    //说明要从最右边的多余视图开始滚动了，最右边的多余视图实际上就是第一个视图。所以偏移量需要更改为第一个视图的偏移量。
    if (offset_X > CGRectGetWidth(self.bounds)*2) {
        self.homePageScrollView.contentOffset = CGPointMake(0, 0);
    }
    //得到最终的偏移量
    CGPoint resultPoint = CGPointMake(offset_X, 0);
    //切换视图时带动画效果
    //最右边的多余视图实际上就是第一个视图，现在是要从第一个视图向第二个视图偏移，所以偏移量为一个屏幕宽度
    if (offset_X >CGRectGetWidth(self.bounds)*2) {
        [ self.homePageScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }else{
        [ self.homePageScrollView setContentOffset:resultPoint animated:YES];
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGPoint offSet=self.homePageScrollView.contentOffset;
    self.pageControl.currentPage=offSet.x/self.bounds.size.width;
}
#pragma mark ----------------- 公告计时器 -----------
- (void)setAnnouncementArray:(NSArray *)announcementArray {
    _announcementArray = nil;
    _announcementArray = announcementArray;
    announcement_i = 0;
    [self addannouncementTimer];
}
- (void)addannouncementTimer{
    if (self.announcementArrayTimer!=NULL) {
        [self.announcementArrayTimer invalidate];
        self.announcementArrayTimer=NULL;
    }
    self.announcementArrayTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(announcementArrayTimerLabel) userInfo:nil repeats:YES];
}
- (void)announcementArrayTimerLabel {
    if (announcement_i == self.announcementArray.count-1) {
        announcement_i = 0;
    }else{
        announcement_i++;
    }
    if (self.announcementArray.count > 0) {
        self.announcementLabel.text = [NSString stringWithFormat:@"%@",self.announcementArray[announcement_i][@"title"]];
        self.anouncement_id = [NSString stringWithFormat:@"%@",self.announcementArray[announcement_i][@"id"]];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
